package com.car.advertising.activity;

import android.view.View;
import android.widget.Toast;

import com.car.advertising.R;
import com.car.advertising.activity.adapter.PersonalInfoAdapter;
import com.car.advertising.db.UserInfoBiz;
import com.car.advertising.db.UserInfoEntiy;
import com.car.advertising.entity.bean.PersonalBean;
import com.car.advertising.listener.SaveEditListener;
import com.support.widget.layout.BaseRecylerViewToolBarActivity;
import com.support.widget.util.ToastUtil;

import java.util.ArrayList;
import java.util.List;

public class PersonalInformationActivity extends BaseRecylerViewToolBarActivity implements SaveEditListener {

    private List<PersonalBean> persnalList = new ArrayList<>();
    private PersonalInfoAdapter personalInfoAdapter;
    private UserInfoEntiy userInfoEntiy;
    private boolean updateInfo = false;

    @Override
    protected void initToolBar() {
        setTitleClickable(false);
        setSubtitleClickable(true);
        setToolBarBackgroundColor(R.color.color_white);
        setToolBarTitle("个人信息");
        setToolBarTitleColor(R.color.black_bg);
        showRightText();
        setRight_text("编辑");
        setRight_textColor(R.color.black_bg);

        setPullDownEnabled(false);
        setPullUpEnabled(false);
    }

    @Override
    protected void init() {
        userInfoEntiy = UserInfoBiz.getInstance(PersonalInformationActivity.this).loadData();
        getPersnalListData(updateInfo);
        setPersnalList();
    }

    private void setPersnalList() {
        if (personalInfoAdapter == null) {
            personalInfoAdapter = new PersonalInfoAdapter(this, persnalList);
            initRecyclerView(personalInfoAdapter);
            personalInfoAdapter.setCommitListener(itemClickListener);
        } else {
            personalInfoAdapter.refreshList(persnalList);
        }
    }

    private void getPersnalListData(boolean updateInfo) {
        if (userInfoEntiy != null) {
            persnalList.clear();
            persnalList.add(new PersonalBean(1, "头像：", userInfoEntiy.getHeader(), userInfoEntiy.getHeader(), "", updateInfo));
            persnalList.add(new PersonalBean(2, "用户名：", userInfoEntiy.getUserName(), userInfoEntiy.getUserName(), "请输入用户名", updateInfo));
            persnalList.add(new PersonalBean(3, "联系电话：", userInfoEntiy.getMobile(), userInfoEntiy.getMobile(), "请输入电话号码", updateInfo));
            persnalList.add(new PersonalBean(4, "邮箱：", userInfoEntiy.getEmail(), userInfoEntiy.getEmail(), "请输入邮箱地址", updateInfo));
            persnalList.add(new PersonalBean(5, "车主认证：", userInfoEntiy.getLicenseAuth(), null, "", false));
            persnalList.add(new PersonalBean(6, "收件地址：", userInfoEntiy.getAddress(), userInfoEntiy.getAddress(), "请输入您的详细地址", updateInfo));
            persnalList.add(new PersonalBean(7, "提交", userInfoEntiy.getAddress(), userInfoEntiy.getAddress(), "", updateInfo));
        }
    }

    @Override
    protected void rightClick() {
        if (updateInfo) {
            updateInfo = false;
            setRight_text("编辑");
        } else {
            updateInfo = true;
            setRight_text("取消");
        }
        getPersnalListData(updateInfo);
        setPersnalList();
    }

    private PersonalInfoAdapter.PersonalCommitListener itemClickListener = new PersonalInfoAdapter.PersonalCommitListener() {

        @Override
        public void onHeadIcon() {
            if (updateInfo) {
                ToastUtil.show(PersonalInformationActivity.this, "点击了头像按钮", Toast.LENGTH_LONG);
            }
        }

        @Override
        public void onPersonalCommit(int position) {
            ToastUtil.show(PersonalInformationActivity.this, "第" + position + "项：点击了提交按钮", Toast.LENGTH_LONG);
        }

    };

    @Override
    public void getEditPositionText(int position, String string) {
        ToastUtil.show(this, "第" + position + "行：输入的是" + string, Toast.LENGTH_LONG);
    }

    @Override
    protected boolean isClickable() {
        return false;
    }

    @Override
    protected void subtitleClick() {

    }

    @Override
    protected void titleClick() {

    }


    @Override
    public String getCN() {
        return null;
    }

    @Override
    public void LogoutCallback(boolean islogin) {

    }

    @Override
    public void onCritClick(View v) {

    }

    @Override
    protected void onRefreshFinish() {

    }

    @Override
    protected void onLoadMoreFinish() {

    }

}
